//package com.freight.core.common.aop;
//
//import com.freight.core.common.annotation.SecretAnnotation;
//import com.freight.core.common.utils.AESUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpInputMessage;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
//
//import java.io.*;
//import java.lang.reflect.Type;
//
///**
// * @author hytma
// * @desc 请求数据解密
// */
//@Slf4j
//@RestControllerAdvice
//public class DecodeRequestBodyAdvice implements RequestBodyAdvice{
//
//    @Override
//    public boolean supports(MethodParameter methodParameter, Type targetType,
//                            Class<? extends HttpMessageConverter<?>> converterType) {
//        //这里设置成false 它就不会再走这个类了
//        return true;
//    }
//    @Override
//    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
//                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
//        StringBuilder stringBuilder = new StringBuilder();
//        BufferedReader bufferedReader = null;
//        try {
//            //这个request其实就是入参 可以从这里获取流
//            //入参放在HttpInputMessage里面  这个方法的返回值也是HttpInputMessage
//            InputStream inputStream = inputMessage.getBody();
//            if (inputStream != null) {
//                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                char[] charBuffer = new char[128];
//                int bytesRead = -1;
//                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
//                    stringBuilder.append(charBuffer, 0, bytesRead);
//                }
//            } else {
//                stringBuilder.append("");
//            }
//        } catch (IOException e) {
//            throw e;
//        } finally {
//            if (bufferedReader != null) {
//                try {
//                    bufferedReader.close();
//                } catch (IOException e) {
//                    throw e;
//                }
//            }
//        }
//      /****   解密     */
//        boolean decode = false;
//        if (parameter.getMethod().isAnnotationPresent(SecretAnnotation.class)) {
//            //获取注解配置的包含和去除字段
//        	SecretAnnotation serializedField = parameter.getMethodAnnotation(SecretAnnotation.class);
//            //出参是否需要加密
//            decode = serializedField.decode();
//        }
//        if(decode){
//        	//获取请求数据
//            String builderString = stringBuilder.toString();
//            log.info("【接受的请求数据】",builderString);
//    		try {
//    			String decodeString = AESUtil.decrypt(builderString);
//    			log.info("【解密后的请求数据】",decodeString);
//    			//把数据放到我们封装的对象中
//    	        return new MyHttpInputMessage(inputMessage.getHeaders(), new ByteArrayInputStream(decodeString.getBytes("UTF-8")));
//    		} catch (Exception e) {
//    			e.printStackTrace();
//    			return null;
//    		}
//        }
//        return null;
//
//
//    }
//
//    @Override
//    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
//                                Class<? extends HttpMessageConverter<?>> converterType) {
//        return body;
//    }
//
//    @Override
//    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
//                                  Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
//        return body;
//    }
//    //这里实现了HttpInputMessage 封装一个自己的HttpInputMessage
//    static class MyHttpInputMessage implements HttpInputMessage {
//        HttpHeaders headers;
//        InputStream body;
//
//        public MyHttpInputMessage(HttpHeaders headers, InputStream body) {
//            this.headers = headers;
//            this.body = body;
//        }
//
//        @Override
//        public InputStream getBody() throws IOException {
//            return body;
//        }
//
//        @Override
//        public HttpHeaders getHeaders() {
//            return headers;
//        }
//    }
//
//}